<!DOCTYPE html>
<html lang="zh-cn">
<head>
  
    <link type="text/css" rel="stylesheet" href="/bundles/blog-common.css?v=KOZafwuaDasEedEenI5aTy8aXH0epbm6VUJ0v3vsT_Q1"/>
<link id="MainCss" type="text/css" rel="stylesheet" href="/skins/ThinkInside/bundle-ThinkInside.css?v=RRjf6pEarGnbXZ86qxNycPfQivwSKWRa4heYLB15rVE1"/>
<link type="text/css" rel="stylesheet" href="/blog/customcss/428549.css?v=%2fam3bBTkW5NBWhBE%2fD0lcyJv5UM%3d"/>

</head>
<body>
<a name="top"></a>

<div id="page_begin_html"></div><script>load_page_begin_html();</script>

<div id="topics">
	<div class = "post">
		<h1 class = "postTitle">
			<a id="cb_post_title_url" class="postTitle2" href="https://www.cnblogs.com/frankdeng/p/9332618.html">Redis（二）CentOS7安装Redis4.0.10与集群搭建</a>
		</h1>
		<div class="clear"></div>
		<div class="postBody">
			<div id="cnblogs_post_body" class="blogpost-body"><h2>一 Redis单机安装</h2>
<h3>1 Redis下载安装</h3>
<h4 class="prettyprint prettyprinted"><span class="pln">1.1 检查依赖环境（Redis是C语言开发，编译依赖gcc环境）</span></h4>
<div class="cnblogs_code">
<pre>[root@node21 redis-<span style="color: #800080;">4.0</span>.<span style="color: #800080;">10</span>]$ gcc -<span style="color: #000000;">v
</span>-<span style="color: #000000;">bash: gcc: command not found
[root@node21 redis</span>-<span style="color: #800080;">4.0</span>.<span style="color: #800080;">10</span>]$ yum install -y gcc</pre>
</div>
<h4 class="prettyprint prettyprinted"><span class="pln">1.2 解压文件到指定目录</span></h4>
<div class="cnblogs_code">
<pre>[root@node21 software]$ wget http:<span style="color: #008000;">//</span><span style="color: #008000;">download.redis.io/releases/redis-4.0.10.tar.gz</span>
<span style="color: #000000;">[root@node21 software]$ ll
</span>-rw-rw-r-- <span style="color: #800080;">1</span> root root   <span style="color: #800080;">1738465</span> June <span style="color: #800080;">14</span> <span style="color: #800080;">00</span>:<span style="color: #800080;">51</span> redis-<span style="color: #800080;">4.0</span>.<span style="color: #800080;">10</span><span style="color: #000000;">.tar.gz
[root@node21 software]$ tar xzf redis</span>-<span style="color: #800080;">4.0</span>.<span style="color: #800080;">10</span>.tar.gz -C /opt/module/</pre>
</div>
<h4 class="prettyprint prettyprinted"><span class="pln">1.3 编译文件</span></h4>
<div class="cnblogs_code">
<pre>[root@node21 software]$ cd /opt/module/redis-<span style="color: #800080;">4.0</span>.<span style="color: #800080;">10</span>/<span style="color: #000000;">
[root@node21 redis</span>-<span style="color: #800080;">4.0</span>.<span style="color: #800080;">10</span>]$ make</pre>
</div>
<p>1）如果没有安装gcc，编译会出现如下错误提示。（如果编译失败，必须删除文件夹，重新解压）</p>
<div class="cnblogs_code">
<pre>/bin/<span style="color: #000000;">sh: cc: command not found
make[</span><span style="color: #800080;">1</span>]: *** [adlist.o] Error <span style="color: #800080;">127</span><span style="color: #000000;">
make[</span><span style="color: #800080;">1</span>]: Leaving directory `/opt/module/redis-<span style="color: #800080;">4.0</span>.<span style="color: #800080;">10</span>/src<span style="color: #800000;">'
</span>make: *** [all] Error <span style="color: #800080;">2</span></pre>
</div>
<p>2）安装成功</p>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201807/1385722-20180720141911861-2093071354.png" alt="" /></p>
<h4 class="prettyprint prettyprinted"><span class="pln">1.4 安装</span></h4>
<div class="cnblogs_code">
<pre>[root@node21 redis-<span style="color: #800080;">4.0</span>.<span style="color: #800080;">10</span>]$ make PREFIX=/opt/module/redis-<span style="color: #800080;">4.0</span>.<span style="color: #800080;">10  </span><span style="color: #000000;">install<br /></span></pre>
</div>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201807/1385722-20180720142010584-1563924254.png" alt="" /></p>
<p>安装完成后，在redis安装目录下会出现一个bin目录，bin目录下有几个可执行文件</p>
<div class="cnblogs_code">
<pre>redis-benchmark         ---<span style="color: #000000;">性能测试工具
redis</span>-check-aof         ---<span style="color: #000000;">AOF文件修复工具
redis</span>-check-rdb         ---<span style="color: #000000;">RDB文件检测工具（快照持久化文件）
redis</span>-cli               ---<span style="color: #000000;">命令行客户端
redis</span>-sentinel -&gt; redis-<span style="color: #000000;">server   ---redis哨兵
redis</span>-server            ---<span style="color: #000000;">redis服务器启动命令<br /></span></pre>
</div>
<h4>1.5 配置环境变量</h4>
<div class="cnblogs_code">
<pre>[root@node21 redis-<span style="color: #800080;">4.0</span>.<span style="color: #800080;">10</span>]$  vi /etc/<span style="color: #000000;">profile
末未追加如下
export  REDIS_HOME</span>=/opt/module/redis-<span style="color: #800080;">4.0</span>.<span style="color: #800080;">10</span><span style="color: #000000;">
export  PATH</span>=$REDIS_HOME/<span style="color: #000000;">bin:$PATH
[root@node21 redis</span>-<span style="color: #800080;">4.0</span>.<span style="color: #800080;">10</span>]# source /etc/profile</pre>
</div>
<h3>2 Redis服务启动</h3>
<h4>2.1 前台模式启动（默认）</h4>
<div class="cnblogs_code">
<pre>[root@node21 redis-4.0.10]$ bin/redis-server</pre>
</div>
<p>前台服务端启动成功后，不能进行其他操作，使用Ctrl+c结束或者服务器关闭redis服务自动结束，此种方式不推荐，成功启动信息如下</p>
<div class="cnblogs_code" onclick="cnblogs_code_show('4b65f64e-f6d5-4537-9d30-275520c2e01a')"><img id="code_img_closed_4b65f64e-f6d5-4537-9d30-275520c2e01a" class="code_img_closed" src="https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif" alt="" /><img id="code_img_opened_4b65f64e-f6d5-4537-9d30-275520c2e01a" class="code_img_opened" style="display: none;" onclick="cnblogs_code_hide('4b65f64e-f6d5-4537-9d30-275520c2e01a',event)" src="https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif" alt="" />
<div id="cnblogs_code_open_4b65f64e-f6d5-4537-9d30-275520c2e01a" class="cnblogs_code_hide">
<pre>[root@node21 redis-<span style="color: #800080;">4.0</span>.<span style="color: #800080;">10</span>]$ bin/redis-<span style="color: #000000;">server 
</span><span style="color: #800080;">5356</span>:C <span style="color: #800080;">19</span> June <span style="color: #800080;">22</span>:<span style="color: #800080;">47</span>:<span style="color: #800080;">00.955</span> # oO0OoO0OoO0Oo Redis <span style="color: #0000ff;">is</span><span style="color: #000000;"> starting oO0OoO0OoO0Oo
</span><span style="color: #800080;">5356</span>:C <span style="color: #800080;">19</span> June <span style="color: #800080;">22</span>:<span style="color: #800080;">47</span>:<span style="color: #800080;">00.956</span> # Redis version=<span style="color: #800080;">4.0</span>.<span style="color: #800080;">10</span>, bits=<span style="color: #800080;">64</span>, commit=<span style="color: #800080;">00000000</span>, modified=<span style="color: #800080;">0</span>, pid=<span style="color: #800080;">5356</span><span style="color: #000000;">, just started
</span><span style="color: #800080;">5356</span>:C <span style="color: #800080;">19</span> June <span style="color: #800080;">22</span>:<span style="color: #800080;">47</span>:<span style="color: #800080;">00.956</span> # Warning: no config file specified, <span style="color: #0000ff;">using</span> the <span style="color: #0000ff;">default</span> config. In order to specify a config file use bin/redis-server /path/to/<span style="color: #000000;">redis.conf
                _._                                                  
           _.</span>-``__ <span style="color: #800000;">''</span>-<span style="color: #000000;">._                                             
      _.</span>-``    `.  `_.  <span style="color: #800000;">''</span>-._           Redis <span style="color: #800080;">4.0</span>.<span style="color: #800080;">10</span> (<span style="color: #800080;">00000000</span>/<span style="color: #800080;">0</span>) <span style="color: #800080;">64</span><span style="color: #000000;"> bit
  .</span>-`` .-```.  ```\/    _.,_ <span style="color: #800000;">''</span>-<span style="color: #000000;">._                                   
 (    </span><span style="color: #800000;">'</span><span style="color: #800000;">      ,       .-`  | `,    )     Running in standalone mode</span>
 |`-._`-...-` __...-.``-._|<span style="color: #800000;">'</span><span style="color: #800000;">` _.-</span><span style="color: #800000;">'</span>|     Port: <span style="color: #800080;">6379</span>
 |    `-._   `._    /     _.-<span style="color: #800000;">'</span><span style="color: #800000;">    |     PID: 5356</span>
  `-._    `-._  `-./  _.-<span style="color: #800000;">'</span><span style="color: #800000;">    _.-</span><span style="color: #800000;">'</span>                                   
 |`-._`-._    `-.__.-<span style="color: #800000;">'</span><span style="color: #800000;">    _.-</span><span style="color: #800000;">'</span>_.-<span style="color: #800000;">'</span><span style="color: #800000;">|                                  </span>
 |    `-._`-._        _.-<span style="color: #800000;">'</span><span style="color: #800000;">_.-</span><span style="color: #800000;">'</span>    |           http:<span style="color: #008000;">//</span><span style="color: #008000;">redis.io        </span>
  `-._    `-._`-.__.-<span style="color: #800000;">'</span><span style="color: #800000;">_.-</span><span style="color: #800000;">'</span>    _.-<span style="color: #800000;">'</span>                                   
 |`-._`-._    `-.__.-<span style="color: #800000;">'</span><span style="color: #800000;">    _.-</span><span style="color: #800000;">'</span>_.-<span style="color: #800000;">'</span><span style="color: #800000;">|                                  </span>
 |    `-._`-._        _.-<span style="color: #800000;">'</span><span style="color: #800000;">_.-</span><span style="color: #800000;">'</span>    |<span style="color: #000000;">                                  
  `</span>-._    `-._`-.__.-<span style="color: #800000;">'</span><span style="color: #800000;">_.-</span><span style="color: #800000;">'</span>    _.-<span style="color: #800000;">'</span>                                   
      `-._    `-.__.-<span style="color: #800000;">'</span><span style="color: #800000;">    _.-</span><span style="color: #800000;">'</span><span style="color: #000000;">                                       
          `</span>-._        _.-<span style="color: #800000;">'</span>                                           
              `-.__.-<span style="color: #800000;">'</span>                                               

<span style="color: #800080;">5356</span>:M <span style="color: #800080;">19</span> June <span style="color: #800080;">22</span>:<span style="color: #800080;">47</span>:<span style="color: #800080;">00.960</span> # WARNING: The TCP backlog setting of <span style="color: #800080;">511</span> cannot be enforced because /proc/sys/net/core/somaxconn <span style="color: #0000ff;">is</span> <span style="color: #0000ff;">set</span> to the lower value of <span style="color: #800080;">128</span><span style="color: #000000;">.
</span><span style="color: #800080;">5356</span>:M <span style="color: #800080;">19</span> June <span style="color: #800080;">22</span>:<span style="color: #800080;">47</span>:<span style="color: #800080;">00.960</span><span style="color: #000000;"> # Server initialized
</span><span style="color: #800080;">5356</span>:M <span style="color: #800080;">19</span> June <span style="color: #800080;">22</span>:<span style="color: #800080;">47</span>:<span style="color: #800080;">00.960</span> # WARNING overcommit_memory <span style="color: #0000ff;">is</span> <span style="color: #0000ff;">set</span> to <span style="color: #800080;">0</span>! Background save may fail under low memory condition. To fix <span style="color: #0000ff;">this</span> issue add <span style="color: #800000;">'</span><span style="color: #800000;">vm.overcommit_memory = </span>
<span style="color: #800080;">1</span><span style="color: #800000;">'</span><span style="color: #800000;"> to /etc/sysctl.conf and then reboot or run the command </span><span style="color: #800000;">'</span>sysctl vm.overcommit_memory=<span style="color: #800080;">1</span><span style="color: #800000;">'</span><span style="color: #800000;"> for this to take effect.5356:M 19 June 22:47:00.960 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis.</span>
 To fix <span style="color: #0000ff;">this</span> issue run the command <span style="color: #800000;">'</span><span style="color: #800000;">echo never &gt; /sys/kernel/mm/transparent_hugepage/enabled</span><span style="color: #800000;">'</span> <span style="color: #0000ff;">as</span> root, and add it to your /etc/rc.local <span style="color: #0000ff;">in</span> order to retain the setting after a reboot. Redis must be restarted after THP <span style="color: #0000ff;">is</span> disabled.<span style="color: #800080;">5356</span>:M <span style="color: #800080;">19</span> June <span style="color: #800080;">22</span>:<span style="color: #800080;">47</span>:<span style="color: #800080;">00.960</span> * Ready to accept connections</pre>
</div>
<span class="cnblogs_code_collapse">View Code</span></div>
<h4>2.2 后台模式启动</h4>
<p>修改redis.conf&nbsp; 将daemonize no&nbsp; 改为 daemonize yes，说明：将Redis设置为后台进程；绑定ip由本地ip换成真机ip，方便对外访问</p>
<div class="cnblogs_code">
<pre>[root@node21 redis-4.0.10]# vi redis.conf</pre>
</div>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201807/1385722-20180720134435279-326818776.png" alt="" /></p>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201807/1385722-20180721192223082-1774635996.png" alt="" /></p>
<p>修改完之后指定配置文件为后台启动</p>
<div class="cnblogs_code">
<pre>[root@node21 redis-4.0.10]# ./bin/redis-<span>server ./redis.conf <br /></span><span style="color: #800080;">5388</span>:C <span style="color: #800080;">19</span> June <span style="color: #800080;">23</span>:<span style="color: #800080;">09</span>:<span style="color: #800080;">44.115</span> # oO0OoO0OoO0Oo Redis <span style="color: #0000ff;">is</span><span> starting oO0OoO0OoO0Oo <br /></span><span style="color: #800080;">5388</span>:C <span style="color: #800080;">19</span> June <span style="color: #800080;">23</span>:<span style="color: #800080;">09</span>:<span style="color: #800080;">44.115</span> # Redis version=<span style="color: #800080;">4.0</span>.<span style="color: #800080;">10</span>, bits=<span style="color: #800080;">64</span>, commit=<span style="color: #800080;">00000000</span>, modified=<span style="color: #800080;">0</span>, pid=<span style="color: #800080;">5388</span><span>, just started <br /></span><span style="color: #800080;">5388</span>:C <span style="color: #800080;">19</span> June <span style="color: #800080;">23</span>:<span style="color: #800080;">09</span>:<span style="color: #800080;">44.115</span> # Configuration loaded</pre>
</div>
<p>Redis默认端口6379，通过当前服务进行查看</p>
<div class="cnblogs_code">
<pre>[root@node21 redis-4.0.10]# ps -ef |grep<span> redis <br />root </span><span style="color: #800080;">5356</span> <span style="color: #800080;">1</span>    <span style="color: #800080;">0</span> <span style="color: #800080;">22</span>:<span style="color: #800080;">47</span> ?     <span style="color: #800080;">00</span>:<span style="color: #800080;">00</span>:<span style="color: #800080;">02</span> bin/redis-server *:<span style="color: #800080;">6379<br /></span><span>root </span><span style="color: #800080;">5391</span> <span style="color: #800080;">5364</span> <span style="color: #800080;">0</span> <span style="color: #800080;">23</span>:<span style="color: #800080;">12</span> pts/<span style="color: #800080;">1</span> <span style="color: #800080;">00</span>:<span style="color: #800080;">00</span>:<span style="color: #800080;">00</span> grep --color=auto -i redis</pre>
</div>
<h3>3 Redis客户端连接</h3>
<h4>连接客户端 ：redis-cli -h ip地址 -p 端口</h4>
<div class="cnblogs_code">
<pre>[root@node21 redis-4.0.10]# ./bin/redis-cli -h 192.168.100.21 -p 6379
192.168.100.21:6379&gt; </pre>
</div>
<p>&nbsp;redis-cli连上redis服务后，可以在命令行发送命令 ping，测试客户端和Redis的连接是否正常，如果连接正常会收到回复pong。</p>
<div class="cnblogs_code">
<pre>192.168.100.21:6379&gt;<span> ping
PONG</span></pre>
</div>
<h3>4 Redis停止</h3>
<p>强制终止Redis进程可能会导致redis持久化数据丢失</p>
<div class="cnblogs_code">
<pre>[root@node21 redis-4.0.10]# kill -9 5336   #pid需要通过&lsquo;ps -aux |grep redis&rsquo; 进行查询</pre>
</div>
<p>正确停止Redis连接方式应向redis发送shutdown指令（关闭默认端口）</p>
<div class="cnblogs_code">
<pre>[root@node21 redis-4.0.10]# redis-cli<span class="pln"><span class="pun"><span class="pln"> <span class="pun">-<span class="pln">h <span class="lit">192.168.100.21<span class="pun"><span class="lit"><span class="pln"> <span class="pun">-<span class="pln">p <span class="lit">6379<span class="pln"> shutdown</span></span></span></span></span></span></span></span></span></span></span></span></span></pre>
</div>
<h4><strong>quit 退出客户端连接</strong></h4>
<h3><strong>5 安装服务脚本</strong></h3>
<div class="cnblogs_code">
<pre>[root@node21 redis-<span style="color: #800080;">4.0</span>.<span style="color: #800080;">10</span>]# cd utils/<span style="color: #000000;">
[root@node21 utils]# .</span>/install_server.sh </pre>
</div>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201807/1385722-20180721210905589-25348567.png" alt="" /></p>
<p><strong>查看服务运行状态</strong></p>
<div class="cnblogs_code">
<pre>[root@node21 utils]# service redis_6379 status</pre>
</div>
<p><img src="https://images2018.cnblogs.com/blog/1385722/201807/1385722-20180720153648961-872818750.png" alt="" /></p>
<p><strong>停止Redis服务</strong></p>
<div class="cnblogs_code">
<pre>[root@node21 admin]# service redis_6379 stop</pre>
</div>
<h2>二 Redis<span style="font-family: 宋体;">集群安装</span>&nbsp;</h2>
<p class="15">右侧是<span style="font-family: 宋体;">官网文档（</span>Redis集群规范<span style="font-family: 宋体;">）&nbsp;</span><a href="http://redis.io/topics/cluster-spec" target="_blank">http://redis.io/topics/cluster-spec</a></p>
<p class="15">右侧是上文<span style="font-family: 宋体;">链接的翻译文档&nbsp;&nbsp;</span><a href="http://redisdoc.com/topic/cluster-spec.html#cluster-spec" target="_blank">http://redisdoc.com/topic/cluster-spec.html#cluster-spec</a></p>
<h2 class="15"><span style="font-size: 16px;">1 原理简述&nbsp;</span></h2>
<p class="15">此处的集群指的是Cluster, 通过分区/分片来实现一定程度的分布式与高可用部署。</p>
<p class="15"><img src="https://images2018.cnblogs.com/blog/1385722/201807/1385722-20180721215624936-811320142.png" alt="" /></p>
<h2><span style="font-size: 16px;">2 集群<span style="font-family: 宋体;">配置</span>&nbsp;</span></h2>
<p class="15"><strong>Redis 最小集群规划，<span><span>需要包含至少三个主节点</span></span>，此处测试部署具有三个主服务器和三个从服务器的六节点群集。<strong><span class="bjh-p">计划是在一台机器上模拟一个集群，</span></strong>主<span style="font-family: 宋体;">节点</span><span style="font-family: 宋体;">端口</span>7000,7001,7002, <span style="font-family: 宋体;">从节点</span>7003,7004,7005，<span style="font-family: 宋体;"><span class="bjh-p">这和生产环境的集群搭建没本质区别。</span></span></strong></p>
<h3><span style="font-size: 15px;">2.1&nbsp;<span style="font-family: 宋体;">创建节点目录</span>&nbsp;</span></h3>
<p>创建一个新目录以及redis实例已端口号命名的以下目录</p>
<div class="cnblogs_code">
<pre>[root@node21 module]# mkdir redis-<span style="color: #000000;">Cluster
[root@node21 module]# cd redis</span>-<span style="color: #000000;">Cluster
[root@node21 redis</span>-Cluster]# mkdir 7000 7001 7002 7003 7004 7005</pre>
</div>
<h3><span style="font-size: 15px;">2.2&nbsp;创建配置文件</span></h3>
<p><span><span>在每个节点目录中</span><span>创建一个</span><span>文件redis.conf，从7000到7005.作为配置文件的模板，以下是最小的Redis群集配置文件，每个节点实例配置稍作修改</span></span></p>
<div class="cnblogs_code">
<pre>[root@node21 redis-Cluster]# cd <span style="color: #800080;">7000</span><span style="color: #000000;">
[root@node21 </span><span style="color: #800080;">7000</span><span style="color: #000000;">]# vi redis.conf
<span style="color: #ff0000;">port </span></span><span style="color: #ff0000;">7000   </span>                    #端口7000,<span style="color: #800080;">7001</span>,<span style="color: #800080;">7002</span>,<span style="color: #800080;">7003</span>,<span style="color: #800080;">7004</span>,<span style="color: #800080;">7005</span><span style="color: #000000;">
cluster</span>-<span style="color: #000000;">enabled yes             #开启集群
cluster</span>-config-<span style="color: #000000;">file nodes.conf  #集群配置信息，开启集群后自动生成
cluster</span>-node-timeout <span style="color: #800080;">5000</span><span style="color: #000000;">       #请求超时时长 
appendonly yes                  #开启aof日志，它会每次写操作都记录一条日志
daemonize yes                   #redis后台运行
bind </span><span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>             #默认ip为127.<span style="color: #800080;">0.0</span>.<span style="color: #800080;">1</span> 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口，无法创建集群  <br /><span style="color: #ff0000;">pidfile  /opt/module/redis-Cluster/7000/redis_7000.pid</span>     #pidfile文件对应7000,<span style="color: #800080;">7001</span>,<span style="color: #800080;">7002</span>,<span style="color: #800080;">7003</span>,<span style="color: #800080;">7004</span>,<span style="color: #800080;">7005</span> <br /><span style="color: #ff0000;">dir  /opt/module/redis-Cluster/7000/</span>    #数据文件存放位置对应7000,7001,7002,7003,7004,7005</pre>
</div>
<h3><span style="font-size: 15px;">2.3&nbsp;<span style="font-family: 宋体;">启动节点服务</span>&nbsp;</span></h3>
<p>分别启动6个实例，可执行文件redis-server用之前单机版的，已配过环境变量</p>
<div class="cnblogs_code">
<pre>[root@node21 redis-Cluster]# redis-server <span style="color: #800080;">7000</span>/<span style="color: #000000;">redis.conf
[root@node21 redis</span>-Cluster]# redis-server <span style="color: #800080;">7001</span>/<span style="color: #000000;">redis.conf
[root@node21 redis</span>-Cluster]# redis-server <span style="color: #800080;">7002</span>/<span style="color: #000000;">redis.conf
[root@node21 redis</span>-Cluster]# redis-server <span style="color: #800080;">7003</span>/<span style="color: #000000;">redis.conf
[root@node21 redis</span>-Cluster]# redis-server <span style="color: #800080;">7004</span>/<span style="color: #000000;">redis.conf
[root@node21 redis</span>-Cluster]# redis-server <span style="color: #800080;">7005</span>/redis.conf</pre>
</div>
<p><strong>6台实例运行起来后，查看进程，下一步创建集群</strong></p>
<div class="cnblogs_code">
<pre>[root@node21 redis-Cluster]# ps -ef |<span style="color: #000000;">grep redis 
root       </span><span style="color: #800080;">9240</span>      <span style="color: #800080;">1</span>  <span style="color: #800080;">0</span> <span style="color: #800080;">18</span>:<span style="color: #800080;">11</span> ?        <span style="color: #800080;">00</span>:<span style="color: #800080;">00</span>:<span style="color: #800080;">00</span> redis-server <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7000</span><span style="color: #000000;"> [cluster]
root       </span><span style="color: #800080;">9245</span>      <span style="color: #800080;">1</span>  <span style="color: #800080;">0</span> <span style="color: #800080;">18</span>:<span style="color: #800080;">11</span> ?        <span style="color: #800080;">00</span>:<span style="color: #800080;">00</span>:<span style="color: #800080;">01</span> redis-server <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7001</span><span style="color: #000000;"> [cluster]
root       </span><span style="color: #800080;">9265</span>      <span style="color: #800080;">1</span>  <span style="color: #800080;">0</span> <span style="color: #800080;">18</span>:<span style="color: #800080;">14</span> ?        <span style="color: #800080;">00</span>:<span style="color: #800080;">00</span>:<span style="color: #800080;">00</span> redis-server <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7002</span><span style="color: #000000;"> [cluster]
root       </span><span style="color: #800080;">9270</span>      <span style="color: #800080;">1</span>  <span style="color: #800080;">0</span> <span style="color: #800080;">18</span>:<span style="color: #800080;">14</span> ?        <span style="color: #800080;">00</span>:<span style="color: #800080;">00</span>:<span style="color: #800080;">00</span> redis-server <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7003</span><span style="color: #000000;"> [cluster]
root       </span><span style="color: #800080;">9275</span>      <span style="color: #800080;">1</span>  <span style="color: #800080;">0</span> <span style="color: #800080;">18</span>:<span style="color: #800080;">14</span> ?        <span style="color: #800080;">00</span>:<span style="color: #800080;">00</span>:<span style="color: #800080;">00</span> redis-server <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7004</span><span style="color: #000000;"> [cluster]
root       </span><span style="color: #800080;">9280</span>      <span style="color: #800080;">1</span>  <span style="color: #800080;">0</span> <span style="color: #800080;">18</span>:<span style="color: #800080;">14</span> ?        <span style="color: #800080;">00</span>:<span style="color: #800080;">00</span>:<span style="color: #800080;">00</span> redis-server <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7005</span> [cluster]</pre>
</div>
<h3><span style="font-size: 15px;">2.4&nbsp;<span style="font-family: 宋体;">创建集群</span></span></h3>
<p>redis安装目录src中redis-<span>trib.rb完成集群创建，</span><span style="font-family: 宋体;">redis-trib.rb命令需要安装gem redis模块才能运行，gem redis需要Ruby环境</span></p>
<div class="cnblogs_code">
<pre><span style="color: #000000;">[root@node21 redis-Cluster]# yum install ruby rubygems </span>-<span style="color: #000000;">y
[root@node21 redis-Cluster]# gem install redis</span></pre>
</div>
<p>&nbsp;创建新集群命令：<span><span>命令</span></span><strong><span><span>create</span></span></strong><span><span>，</span><span>选项</span></span><code>--replicas 1</code><span><span>意味着我们希望每个创建的主服务器都有一个从服&nbsp;</span><span>其他参数是我要用于创建新集群的实例的地址列表。</span></span></p>
<div class="cnblogs_code">
<pre><span style="color: #000000;">[root@node21 src]# pwd
</span>/opt/module/redis-<span style="color: #800080;">4.0</span>.<span style="color: #800080;">10</span>/<span style="color: #000000;">src
[root@node21 src]# .</span>/redis-trib.rb create --replicas <span style="color: #800080;">1</span> <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7000</span> <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7001</span> <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7002</span> <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7003</span> <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7004</span> <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7005</span></pre>
</div>
<p>创建命令运行界面如下，主要是分配主从和槽位数</p>
<div class="cnblogs_code">
<pre>&gt;&gt;&gt;<span style="color: #ff0000;"> Creating cluster
</span>&gt;&gt;&gt; <span style="color: #ff0000;">Performing hash slots allocation on 6</span><span style="color: #000000;"><span style="color: #ff0000;"> nodes</span>...
<span style="color: #ff0000;">Using </span></span><span style="color: #ff0000;">3 masters:
</span>192.168.100.21:<span style="color: #800080;">7000</span>
192.168.100.21:<span style="color: #800080;">7001</span>
192.168.100.21:<span style="color: #800080;">7002</span><span style="color: #ff0000;">
Adding replica 192.168.100.21:7004 to 192.168.100.21:7000
Adding replica 192.168.100.21:7005 to 192.168.100.21:7001
Adding replica 192.168.100.21:7003 to 192.168.100.21:7002</span>
&gt;&gt;&gt; Trying to optimize slaves allocation <span style="color: #0000ff;">for</span> anti-<span style="color: #000000;">affinity
[WARNING] Some slaves are </span><span style="color: #0000ff;">in</span> the same host <span style="color: #0000ff;">as</span><span style="color: #000000;"> their master
M: 2b2a9cab4591508e44290a1fd16781e5c9099010 192.168.100.21</span>:<span style="color: #800080;">7000</span><span style="color: #000000;">
   slots:</span><span style="color: #800080;">0</span>-<span style="color: #800080;">5460</span> (<span style="color: #800080;">5461</span><span style="color: #000000;"> slots) master
M: 69d97b0c7c3c1587f0e4ed0abb82c01fa66a5478 192.168.100.21</span>:<span style="color: #800080;">7001</span><span style="color: #000000;">
   slots:</span><span style="color: #800080;">5461</span>-<span style="color: #800080;">10922</span> (<span style="color: #800080;">5462</span><span style="color: #000000;"> slots) master
M: 4728f1d7da089b6ec9c20f8c9907652536ee5804 192.168.100.21</span>:<span style="color: #800080;">7002</span><span style="color: #000000;">
   slots:</span><span style="color: #800080;">10923</span>-<span style="color: #800080;">16383</span> (<span style="color: #800080;">5461</span><span style="color: #000000;"> slots) master
S: 73f894c5ddca484f5b72fd7f1f08612e50681133 192.168.100.21</span>:<span style="color: #800080;">7003</span><span style="color: #000000;">
   replicates 4728f1d7da089b6ec9c20f8c9907652536ee5804
S: 7f09c99b30f16ed583f9e144c75e2bb065c0528d 192.168.100.2</span><span style="color: #800080;">1</span>:<span style="color: #800080;">7004</span><span style="color: #000000;">
   replicates 2b2a9cab4591508e44290a1fd16781e5c9099010
S: 23d31961a1ad249b4af1d9f7be6cf3abc1b82f0a 192.168.100.21</span>:<span style="color: #800080;">7005</span><span style="color: #000000;">
   replicates 69d97b0c7c3c1587f0e4ed0abb82c01fa66a5478
Can I </span><span style="color: #0000ff;">set</span> the above configuration? (type <span style="color: #800000;">'</span><span style="color: #800000;">yes</span><span style="color: #800000;">'</span><span style="color: #000000;"> to accept): <span style="color: #ff0000;">yes
</span></span>&gt;&gt;&gt;<span style="color: #000000;"> Nodes configuration updated
</span>&gt;&gt;&gt;<span style="color: #000000;"> Assign a different config epoch to each node
</span>&gt;&gt;&gt;<span style="color: #000000;"> Sending CLUSTER MEET messages to join the cluster
Waiting </span><span style="color: #0000ff;">for</span><span style="color: #000000;"> the cluster to join..
</span>&gt;&gt;&gt; Performing Cluster Check (<span style="color: #0000ff;">using</span> node 192.168.100.21:<span style="color: #800080;">7000</span><span style="color: #000000;">)
M: 2b2a9cab4591508e44290a1fd16781e5c9099010 192.168.100.21</span>:<span style="color: #ff0000;">7000
   slots:0-5460 (5461 slots) master
   </span><span style="color: #800080;">1</span><span style="color: #000000;"> additional replica(s)
S: 73f894c5ddca484f5b72fd7f1f08612e50681133 </span><span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7003</span><span style="color: #000000;">
   slots: (</span><span style="color: #800080;">0</span><span style="color: #000000;"> slots) slave
   replicates 4728f1d7da089b6ec9c20f8c9907652536ee5804
S: 7f09c99b30f16ed583f9e144c75e2bb065c0528d </span><span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7004</span><span style="color: #000000;">
   slots: (</span><span style="color: #800080;">0</span><span style="color: #000000;"> slots) slave
   replicates 2b2a9cab4591508e44290a1fd16781e5c9099010
S: 23d31961a1ad249b4af1d9f7be6cf3abc1b82f0a </span><span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7005</span><span style="color: #000000;">
   slots: (</span><span style="color: #800080;">0</span><span style="color: #000000;"> slots) slave
   replicates 69d97b0c7c3c1587f0e4ed0abb82c01fa66a5478
M: 69d97b0c7c3c1587f0e4ed0abb82c01fa66a5478 </span><span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #ff0000;">7001
   slots:5461-10922 (5462 slots) master
   </span><span style="color: #800080;">1</span><span style="color: #000000;"> additional replica(s)
M: 4728f1d7da089b6ec9c20f8c9907652536ee5804 </span><span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #ff0000;">7002
   slots:10923-16383 (5461 slots) master
   </span><span style="color: #800080;">1</span><span style="color: #000000;"> additional replica(s)
<span style="color: #ff0000;">[OK] All nodes agree about slots configuration.
</span></span>&gt;&gt;&gt; Check <span style="color: #0000ff;">for</span><span style="color: #000000;"> open slots...
</span>&gt;&gt;&gt;<span style="color: #000000;"> Check slots coverage...
<span style="color: #ff0000;">[OK] All </span></span><span style="color: #ff0000;">16384 slots covered.</span></pre>
</div>
<h2><span style="font-size: 16px;">3 集群测试</span></h2>
<p>&nbsp;集群客户端连接方式 redis-cli&nbsp;&nbsp;-c&nbsp;</p>
<div class="cnblogs_code">
<pre>[root@node21 redis-Cluster]# redis-cli -c -h 192.168.100.21 -p <span style="color: #800080;">7000</span>
192.168.100.21:<span style="color: #800080;">7000</span>&gt; <span style="color: #0000ff;">set</span><span style="color: #000000;"> foo bar
</span>-&gt; Redirected to slot [<span style="color: #800080;">12182</span>] located at <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7002</span><span style="color: #000000;">
OK
</span><span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7002</span>&gt; <span style="color: #0000ff;">get</span><span style="color: #000000;"> foo
</span><span style="color: #800000;">"</span><span style="color: #800000;">bar</span><span style="color: #800000;">"</span>
<span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7002</span>&gt; keys *
<span style="color: #800080;">1</span>) <span style="color: #800000;">"</span><span style="color: #800000;">foo</span><span style="color: #800000;">"</span>
<span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7002</span>&gt;<span style="color: #000000;"> exit
[root@node21 redis</span>-Cluster]# redis-cli -c -h 192.168.100.21 -p <span style="color: #800080;">7000</span>
192.168.100.21:<span style="color: #800080;">7000</span>&gt; keys *<span style="color: #000000;">
(empty list or </span><span style="color: #0000ff;">set</span><span style="color: #000000;">)
</span>192.168.100.21:<span style="color: #800080;">7000</span>&gt; <span style="color: #0000ff;">get</span><span style="color: #000000;"> foo
</span>-&gt; Redirected to slot [<span style="color: #800080;">12182</span>] located at <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7002</span>
<span style="color: #800000;">"</span><span style="color: #800000;">bar</span><span style="color: #800000;">"</span>
<span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7002</span>&gt; </pre>
</div>
<p>不加-c会报错如下</p>
<div class="cnblogs_code">
<pre>[root@node21 redis-Cluster]# redis-cli -p <span style="color: #800080;">7000</span>
192.168.100.21:<span style="color: #800080;">7000</span>&gt; <span style="color: #0000ff;">set</span><span style="color: #000000;"> foo bar
(error) MOVED </span><span style="color: #800080;">12182</span> <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7002</span></pre>
</div>
<h2><span style="font-size: 16px;">4<span style="font-family: 宋体;">验证故障转移</span></span></h2>
<p><span style="font-family: 宋体;">我们可以使用以下命令识别集群并使其崩溃：</span></p>
<div class="cnblogs_code">
<pre>[root@node21 redis-Cluster]# redis-cli -h 192.168.100.21 -p <span style="color: #800080;">7000</span> cluster nodes |<span style="color: #000000;"> grep master
2b2a9cab4591508e44290a1fd16781e5c9099010 192.168.100.21</span>:<span style="color: #800080;">7000</span>@<span style="color: #800080;">17000</span> myself,master - <span style="color: #800080;">0</span> <span style="color: #800080;">1532157916000</span> <span style="color: #800080;">1</span> connected <span style="color: #800080;">0</span>-<span style="color: #800080;">5460</span><span style="color: #000000;">
69d97b0c7c3c1587f0e4ed0abb82c01fa66a5478 </span><span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7001</span>@<span style="color: #800080;">17001</span> master - <span style="color: #800080;">0</span> <span style="color: #800080;">1532157917389</span> <span style="color: #800080;">2</span> connected <span style="color: #800080;">5461</span>-<span style="color: #800080;">10922</span><span style="color: #000000;">
4728f1d7da089b6ec9c20f8c9907652536ee5804 </span><span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7002</span>@<span style="color: #800080;">17002</span> master - <span style="color: #800080;">0</span> <span style="color: #800080;">1532157917000</span> <span style="color: #800080;">3</span> connected <span style="color: #800080;">10923</span>-<span style="color: #800080;">16383</span></pre>
</div>
<p>如上，<span><span>7000,7001和7002都是主服。</span><span>让我们使用</span></span><strong><span><span>DEBUG SEGFAULT</span></span></strong><span><span>命令</span><span>崩溃节点7002&nbsp;</span><span>：</span></span></p>
<div class="cnblogs_code">
<pre>[root@node21 redis-Cluster]# redis-cli -h 192.168.100.21 -p <span style="color: #800080;">7002</span><span style="color: #000000;"> debug segfault
Error: Server closed the connection</span></pre>
</div>
<p>7002的从服7003节点部分日志信息如下</p>
<div class="cnblogs_code">
<pre><span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">13</span>:<span style="color: #800080;">51</span>:<span style="color: #800080;">43.245</span> *<span style="color: #000000;"> Background AOF rewrite finished successfully
</span><span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">45.370</span><span style="color: #000000;"> # <span style="color: #ff0000;">Connection with master lost.
</span></span><span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">45.371</span> *<span style="color: #000000;"> Caching the disconnected master state.
</span><span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">45.595</span> * Connecting to MASTER <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7002</span>
<span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">45.595</span> * MASTER &lt;-&gt;<span style="color: #000000;"> SLAVE sync started
</span><span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">45.595</span> # Error condition on socket <span style="color: #0000ff;">for</span><span style="color: #000000;"> SYNC: Connection refused
</span><span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">46.604</span> * Connecting to MASTER <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7002</span>
<span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">46.605</span> * MASTER &lt;-&gt;<span style="color: #000000;"> SLAVE sync started
</span><span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">46.605</span> # Error condition on socket <span style="color: #0000ff;">for</span><span style="color: #000000;"> SYNC: Connection refused
</span><span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">47.999</span> * Connecting to MASTER <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7002</span>
<span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">47.999</span> * MASTER &lt;-&gt;<span style="color: #000000;"> SLAVE sync started
</span><span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">47.999</span> # Error condition on socket <span style="color: #0000ff;">for</span><span style="color: #000000;"> SYNC: Connection refused
</span><span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">49.008</span> * Connecting to MASTER <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7002</span>
<span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">49.008</span> * MASTER &lt;-&gt;<span style="color: #000000;"> SLAVE sync started
</span><span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">49.008</span> # Error condition on socket <span style="color: #0000ff;">for</span><span style="color: #000000;"> SYNC: Connection refused
</span><span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">50.023</span> * Connecting to MASTER <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7002</span>
<span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">50.023</span> * MASTER &lt;-&gt;<span style="color: #000000;"> SLAVE sync started
</span><span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">50.024</span> # Error condition on socket <span style="color: #0000ff;">for</span><span style="color: #000000;"> SYNC: Connection refused
</span><span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">51.032</span> * FAIL message received <span style="color: #0000ff;">from</span><span style="color: #000000;"> 69d97b0c7c3c1587f0e4ed0abb82c01fa66a5478 about 4728f1d7da089b6ec9c20f8c9907652536ee5804
</span><span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">51.032</span> * Connecting to MASTER <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7002</span>
<span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">51.032</span> * MASTER &lt;-&gt;<span style="color: #000000;"> SLAVE sync started
</span><span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">51.032</span> # Start of election delayed <span style="color: #0000ff;">for</span> <span style="color: #800080;">699</span> milliseconds (rank #<span style="color: #800080;">0</span>, offset <span style="color: #800080;">7992</span><span style="color: #000000;">).
</span><span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">51.032</span><span style="color: #000000;"> # Cluster state changed: fail
</span><span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">51.033</span> # Error condition on socket <span style="color: #0000ff;">for</span><span style="color: #000000;"> SYNC: Connection refused
</span><span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">51.738</span> # Starting a failover election <span style="color: #0000ff;">for</span> epoch <span style="color: #800080;">7</span><span style="color: #000000;">.
</span><span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">51.749</span> # Failover election won: I<span style="color: #800000;">'</span><span style="color: #800000;">m the new master.</span>
<span style="color: #800080;">8417</span>:S <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">51.750</span> # configEpoch <span style="color: #0000ff;">set</span> to <span style="color: #800080;">7</span><span style="color: #000000;"> after successful failover
</span><span style="color: #800080;">8417</span>:M <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">51.750</span> # Setting secondary replication ID to 8ef669a5f5a6e6efafed75005ca9426d539c4c47, valid up to offset: <span style="color: #800080;">7993</span>. New replication ID <span style="color: #0000ff;">is</span><span style="color: #000000;"> bec5f6f31031
1deaa4aff371b7244cd131f594fe8417:M </span><span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">51.750</span> *<span style="color: #000000;"> Discarding previously cached master state.
</span><span style="color: #800080;">8417</span>:M <span style="color: #800080;">21</span> Jul <span style="color: #800080;">15</span>:<span style="color: #800080;">27</span>:<span style="color: #800080;">51.750</span> # <span style="color: #ff0000;">Cluster state changed: ok</span></pre>
</div>
<p>客户端测试：7002的数据自动跳转到7003上</p>
<div class="cnblogs_code">
<pre>[root@node21 redis-Cluster]# redis-cli -c -h 192.168.100.21 -p <span style="color: #800080;">7000</span>
192.168.100.21:<span style="color: #800080;">7000</span>&gt; <span style="color: #0000ff;">get</span><span style="color: #000000;"> foo
</span>-&gt; Redirected to slot [<span style="color: #800080;">12182</span>] located at <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7003</span>
<span style="color: #800000;">"</span><span style="color: #800000;">bar</span><span style="color: #800000;">"</span>
<span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7003</span>&gt; </pre>
</div>
<p>查看集群节点状态</p>
<div class="cnblogs_code">
<pre>[root@node21 redis-Cluster]# redis-cli -h 192.168.100.21 -p <span style="color: #800080;">7000</span><span style="color: #000000;"> cluster nodes
2b2a9cab4591508e44290a1fd16781e5c9099010 192.168.100.21</span>:<span style="color: #800080;">7000</span>@<span style="color: #800080;">17000</span> myself,master - <span style="color: #800080;">0</span> <span style="color: #800080;">1532158988000</span> <span style="color: #800080;">1</span> connected <span style="color: #800080;">0</span>-<span style="color: #800080;">5460</span><span style="color: #000000;">
73f894c5ddca484f5b72fd7f1f08612e50681133 </span><span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7003</span>@<span style="color: #800080;">17003</span> master - <span style="color: #800080;">0</span> <span style="color: #800080;">1532158987255</span> <span style="color: #800080;">7</span> connected <span style="color: #800080;">10923</span>-<span style="color: #800080;">16383</span><span style="color: #000000;">
7f09c99b30f16ed583f9e144c75e2bb065c0528d </span><span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7004</span>@<span style="color: #800080;">17004</span> slave 2b2a9cab4591508e44290a1fd16781e5c9099010 <span style="color: #800080;">0</span> <span style="color: #800080;">1532158987558</span> <span style="color: #800080;">5</span><span style="color: #000000;"> connected
23d31961a1ad249b4af1d9f7be6cf3abc1b82f0a </span><span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7005</span>@<span style="color: #800080;">17005</span> slave 69d97b0c7c3c1587f0e4ed0abb82c01fa66a5478 <span style="color: #800080;">0</span> <span style="color: #800080;">1532158988769</span> <span style="color: #800080;">6</span><span style="color: #000000;"> connected
69d97b0c7c3c1587f0e4ed0abb82c01fa66a5478 </span><span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7001</span>@<span style="color: #800080;">17001</span> master - <span style="color: #800080;">0</span> <span style="color: #800080;">1532158987558</span> <span style="color: #800080;">2</span> connected <span style="color: #800080;">5461</span>-<span style="color: #800080;">10922</span><span style="color: #000000;">
4728f1d7da089b6ec9c20f8c9907652536ee5804 </span><span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7002</span>@<span style="color: #800080;">17002</span> master,fail - <span style="color: #800080;">1532158065392</span> <span style="color: #800080;">1532158064562</span> <span style="color: #800080;">3</span> disconnected</pre>
</div>
<p>7002再启动，又成为集群中的从服。</p>
<h2><span style="font-size: 16px;">5 其他说明</span></h2>
<h3><span style="font-size: 15px;">5.1 添加新节点为主服</span></h3>
<p>&nbsp;使用<strong>add-node</strong>命令将新节点的地址指定为第一个参数，并将集群中随机存在节点的地址指定为第二个参数，redis-trib在运行之前也会检查集群的状态。</p>
<div class="cnblogs_code">
<pre># ./redis-trib.rb add-node 192.168.100.21:<span style="color: #800080;">7006</span> 192.168.100.21:<span style="color: #800080;">7000</span></pre>
</div>
<p>当添加新节点为主节点时，</p>
<ul>
<li><span>它没有数据，因为它没有分配的哈希槽。</span></li>
<li><span>因为它是没有分配插槽的主设备，所以当从设备想要成为主设备时，它不参与选举过程。</span></li>
</ul>
<p><span><span>现在可以使用resharding功能为此节点分配哈希槽</span></span><code>redis-trib</code><span><span>，</span><span>重新分区，具体参考官方文档，这里不详述了。</span></span></p>
<h3><span style="font-size: 15px;">5.2&nbsp;添加新节点为副本</span></h3>
<p>添加新副本分配给指定的主副本</p>
<div class="cnblogs_code">
<pre>./redis-trib.rb add-node --slave --master-id 3c3a0c74aae0b56170ccb03a76b60cfe7dc1912e 192.168.100.21:<span style="color: #800080;">7006</span> 192.168.100.21:<span style="color: #800080;">7000</span></pre>
</div>
<h2><span style="font-size: 18px;">三 安装报错解决</span></h2>
<h3><span style="font-size: 15px;">1 gem安装超时</span></h3>
<p class="title-article">解决gem install redis 因为网络问题造成连接超时的问题，把gem镜像换成国内淘宝的</p>
<div class="cnblogs_code">
<pre>[root@node21 redis-Cluster]# gem sources -<span style="color: #000000;">l
</span>*** CURRENT SOURCES ***<span style="color: #000000;">

https:</span><span style="color: #008000;">//</span><span style="color: #008000;">rubygems.org/</span>
[root@node21 redis-Cluster]# gem sources --remove https:<span style="color: #008000;">//</span><span style="color: #008000;">rubygems.org/</span>
https:<span style="color: #008000;">//</span><span style="color: #008000;">rubygems.org/ removed from sources</span>
[root@node21 redis-Cluster]# gem sources -a https:<span style="color: #008000;">//</span><span style="color: #008000;">ruby.taobao.org/</span>
https:<span style="color: #008000;">//</span><span style="color: #008000;">ruby.taobao.org/ added to sources</span>
[root@node21 redis-Cluster]# gem sources -<span style="color: #000000;">l
</span>*** CURRENT SOURCES ***<span style="color: #000000;">

https:</span><span style="color: #008000;">//</span><span style="color: #008000;">ruby.taobao.org/</span></pre>
</div>
<h3><span style="font-size: 15px;">2Ruby版本过低</span></h3>
<div class="cnblogs_code">
<pre>[root@node21 redis-<span style="color: #000000;">Cluster]# gem install redis
Fetching: redis</span>-<span style="color: #800080;">4.0</span>.<span style="color: #800080;">1</span>.gem (<span style="color: #800080;">100</span>%<span style="color: #000000;">)
ERROR:  Error installing redis:
    redis requires Ruby version </span>&gt;= <span style="color: #800080;">2.2</span>.<span style="color: #800080;">2</span>.</pre>
</div>
<p>我这里CentOS7.5默认安装的Ruby版本为2.0，需升级为2.2.2以上（ruby -v),先安装RVM，用RVM升级Ruby</p>
<div class="cnblogs_code">
<pre><span style="color: #800080;">1</span><span style="color: #000000;">、sudo yum install curl  安装curl
</span><span style="color: #800080;">2</span>、http:<span style="color: #008000;">//</span><span style="color: #008000;">www.rvm.io/ 官网首页就可以看到 $ gpg --keyserver hkp:</span><span style="color: #008000;">//</span><span style="color: #008000;">keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB</span>
<span style="color: #800080;">3</span>、下载rvm  $ \curl -sSL https:<span style="color: #008000;">//</span><span style="color: #008000;">get.rvm.io | bash -s stable </span>
<span style="color: #800080;">4</span>、查找配置文件 find / -<span style="color: #000000;">name rvm.sh 
</span><span style="color: #800080;">5</span>、配置文件生效 source /etc/profile.d/<span style="color: #000000;">rvm.sh 
</span><span style="color: #800080;">6</span><span style="color: #000000;">、下载rvm依赖 rvm requirements 
</span><span style="color: #800080;">7</span><span style="color: #000000;">、查看rvm库ruby版本 rvm list known
</span><span style="color: #800080;">8</span>、安装ruby指定版本 rvm install ruby-<span style="color: #800080;">2.5</span>.<span style="color: #800080;">1</span>
<span style="color: #800080;">9</span>、使用ruby版本默认 rvm use <span style="color: #800080;">2.5</span>.<span style="color: #800080;">1</span> <span style="color: #0000ff;">default<br />10.gem install redis</span></pre>
</div>
<h3><span style="font-size: 15px;">3创建集群报错</span></h3>
<p>集群数据不为空，Node 192.168.100.21:7001 is not empty&nbsp;</p>
<div class="cnblogs_code">
<pre>[root@node21 src]# ./redis-trib.rb create --replicas <span style="color: #800080;">1</span> <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7000</span> <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7001</span> <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7002</span> <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7003</span> <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7004</span> <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100</span><span style="color: #000000;">.
</span><span style="color: #800080;">21</span>:<span style="color: #800080;">7005</span>&gt;&gt;&gt;<span style="color: #000000;"> Creating cluster
[ERR] <span style="color: #ff0000;">Node </span></span><span style="color: #ff0000;">192.168.100.21:7001 is not empty.</span> Either the node already knows other nodes (check with CLUSTER NODES) or <span style="color: #ff0000;">contains some key in database 0.</span></pre>
</div>
<p>清空数据，在将每个节点下aof、rdb、nodes.conf本地备份文件删除 ，重新创建即可成功</p>
<div class="cnblogs_code">
<pre><span style="color: #000000;">[root@node21 src]# redis</span>-cli -h <span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span> -p <span style="color: #800080;">7001</span>
<span style="color: #800080;">192.168</span>.<span style="color: #800080;">100.21</span>:<span style="color: #800080;">7001</span>&gt;<span style="color: #000000;"> flushdb
OK</span></pre>
</div>
<h2>四Redis配置说明</h2>
<p>Redis 的配置文件位于 Redis 安装目录下，文件名为 redis.conf，配置项说明如下：</p>
<p>1. Redis默认不是以守护进程的方式运行，可以通过该配置项修改，使用yes启用守护进程</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>daemonize no</strong></p>
<p>2. 当Redis以守护进程方式运行时，Redis默认会把pid写入/var/run/redis.pid文件，可以通过pidfile指定</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>pidfile /var/run/redis.pid</strong></p>
<p>3. 指定Redis监听端口，默认端口为6379，</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>port 6379</strong></p>
<p>4. 绑定的主机地址</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>bind 127.0.0.1</strong></p>
<p>5.当 客户端闲置多长时间后关闭连接，如果指定为0，表示关闭该功能</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>timeout 300</strong></p>
<p>6. 指定日志记录级别，Redis总共支持四个级别：debug、verbose、notice、warning，默认为verbose</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>loglevel verbose</strong></p>
<p>7. 日志记录方式，默认为标准输出，如果配置Redis为守护进程方式运行，而这里又配置为日志记录方式为标准输出，则日志将会发送给/dev/null</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>logfile stdout</strong></p>
<p>8. 设置数据库的数量，默认数据库为0，可以使用SELECT &lt;dbid&gt;命令在连接上指定数据库id</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>databases 16</strong></p>
<p>9. 指定在多长时间内，有多少次更新操作，就将数据同步到数据文件，可以多个条件配合</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>save &lt;seconds&gt; &lt;changes&gt;</strong></p>
<p>&nbsp;&nbsp;&nbsp; Redis默认配置文件中提供了三个条件：</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>save 900 1</strong></p>
<p><strong>&nbsp;&nbsp;&nbsp;&nbsp;save 300 10</strong></p>
<p><strong>&nbsp;&nbsp;&nbsp;&nbsp;save 60 10000</strong></p>
<p>&nbsp;&nbsp;&nbsp; 分别表示900秒（15分钟）内有1个更改，300秒（5分钟）内有10个更改以及60秒内有10000个更改。</p>
<p>10. 指定存储至本地数据库时是否压缩数据，默认为yes，Redis采用LZF压缩，如果为了节省CPU时间，可以关闭该选项，但会导致数据库文件变的巨大</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>rdbcompression yes</strong></p>
<p>11. 指定本地数据库文件名，默认值为dump.rdb</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>dbfilename dump.rdb</strong></p>
<p>12. 指定本地数据库存放目录</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>dir ./</strong></p>
<p>13. 设置当本机为slav服务时，设置master服务的IP地址及端口，在Redis启动时，它会自动从master进行数据同步</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>slaveof &lt;masterip&gt; &lt;masterport&gt;</strong></p>
<p>14. 当master服务设置了密码保护时，slav服务连接master的密码</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>masterauth &lt;master-password&gt;</strong></p>
<p>15. 设置Redis连接密码，如果配置了连接密码，客户端在连接Redis时需要通过AUTH &lt;password&gt;命令提供密码，默认关闭</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>requirepass foobared</strong></p>
<p>16. 设置同一时间最大客户端连接数，默认无限制，Redis可以同时打开的客户端连接数为Redis进程可以打开的最大文件描述符数，如果设置 maxclients 0，表示不作限制。当客户端连接数到达限制时，Redis会关闭新的连接并向客户端返回max number of clients reached错误信息</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>maxclients 128</strong></p>
<p>17. 指定Redis最大内存限制，Redis在启动时会把数据加载到内存中，达到最大内存后，Redis会先尝试清除已到期或即将到期的Key，当此方法处理 后，仍然到达最大内存设置，将无法再进行写入操作，但仍然可以进行读取操作。Redis新的vm机制，会把Key存放内存，Value会存放在swap区</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>maxmemory &lt;bytes&gt;</strong></p>
<p>18. 指定是否在每次更新操作后进行日志记录，Redis在默认情况下是异步的把数据写入磁盘，如果不开启，可能会在断电时导致一段时间内的数据丢失。因为 redis本身同步数据文件是按上面save条件来同步的，所以有的数据会在一段时间内只存在于内存中。默认为no</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>appendonly no</strong></p>
<p>19. 指定更新日志文件名，默认为appendonly.aof</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>appendfilename appendonly.aof</strong></p>
<p>20. 指定更新日志条件，共有3个可选值：&nbsp;<br />&nbsp; &nbsp;&nbsp;<strong>no</strong>：表示等操作系统进行数据缓存同步到磁盘（快）&nbsp;<br />&nbsp; &nbsp;&nbsp;<strong>always</strong>：表示每次更新操作后手动调用fsync()将数据写到磁盘（慢，安全）&nbsp;<br />&nbsp; &nbsp;&nbsp;<strong>everysec</strong>：表示每秒同步一次（折衷，默认值）</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>appendfsync everysec</strong></p>
<p>21. 指定是否启用虚拟内存机制，默认值为no，简单的介绍一下，VM机制将数据分页存放，由Redis将访问量较少的页即冷数据swap到磁盘上，访问多的页面由磁盘自动换出到内存中（在后面的文章我会仔细分析Redis的VM机制）</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>vm-enabled no</strong></p>
<p>22. 虚拟内存文件路径，默认值为/tmp/redis.swap，不可多个Redis实例共享</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>vm-swap-file /tmp/redis.swap</strong></p>
<p>23. 将所有大于vm-max-memory的数据存入虚拟内存,无论vm-max-memory设置多小,所有索引数据都是内存存储的(Redis的索引数据 就是keys),也就是说,当vm-max-memory设置为0的时候,其实是所有value都存在于磁盘。默认值为0</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>vm-max-memory 0</strong></p>
<p>24. Redis swap文件分成了很多的page，一个对象可以保存在多个page上面，但一个page上不能被多个对象共享，vm-page-size是要根据存储的 数据大小来设定的，作者建议如果存储很多小对象，page大小最好设置为32或者64bytes；如果存储很大大对象，则可以使用更大的page，如果不 确定，就使用默认值</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>vm-page-size 32</strong></p>
<p>25. 设置swap文件中的page数量，由于页表（一种表示页面空闲或使用的bitmap）是在放在内存中的，，在磁盘上每8个pages将消耗1byte的内存。</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>vm-pages 134217728</strong></p>
<p>26. 设置访问swap文件的线程数,最好不要超过机器的核数,如果设置为0,那么所有对swap文件的操作都是串行的，可能会造成比较长时间的延迟。默认值为4</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<strong>vm-max-threads 4</strong></p>
<p>27. 设置在向客户端应答时，是否把较小的包合并为一个包发送，默认为开启</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>glueoutputbuf yes</strong></p>
<p>28. 指定在超过一定的数量或者最大的元素超过某一临界值时，采用一种特殊的哈希算法</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>hash-max-zipmap-entries 64</strong></p>
<p><strong>&nbsp;&nbsp;&nbsp;&nbsp;hash-max-zipmap-value 512</strong></p>
<p>29. 指定是否激活重置哈希，默认为开启（后面在介绍Redis的哈希算法时具体介绍）</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>activerehashing yes</strong></p>
<p>30. 指定包含其它的配置文件，可以在同一主机上多个Redis实例之间使用同一份配置文件，而同时各个实例又拥有自己的特定配置文件</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;<strong>include /path/to/local.conf</strong></p></div>

</body>
</html>
